<?php

use think\migration\Migrator;
use app\model\CouponBrand;
use app\model\CouponCate;
use app\model\CouponBrandCateRelation;

class AlterCouponBrandCate extends Migrator
{
    public function up()
    {
        CouponBrand::query()->whereIn('type', [CouponBrand::TYPE_TRAVEL, CouponBrand::TYPE_UNION_FOOD])->select()->each(function (CouponBrand $couponBrand) {
            $brand_id = $couponBrand->id; //品牌id
            $type = $couponBrand->type;   //类别 出行加油还是餐饮连锁
            if ($type == CouponBrand::TYPE_TRAVEL) {//出行
                $name = '日常出行';
            } else {//餐饮连锁
                $name = '餐饮美食';
            }
            $cate_id = CouponCate::query()->whereLike('name', '%'.$name.'%')->value('id');
            CouponBrandCateRelation::findOrCreate(
                [
                    'brand_id' => $brand_id,
                    'cate_id'  => $cate_id,
                ]);
        });
    }
}
